package org.dea.mapping;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class EasyAcceptMappingReader {

	/**
	 * Process a mapping file and generate a set of DistributedComponents
	 * organized in a Map collection
	 * 
	 * @param mappingFile A File that points to a EasyAccept mapping file
	 * @return DistributedComponents
	 * @throws IOException 
	 * @throws Exception
	 */
	public static Map<String,DistributedComponent> processMappingFile( String mappingFile ) throws IOException {

		Map<String,DistributedComponent> componentsMap = new HashMap<String,DistributedComponent>();

		// Get DistributedComponents
		Document queriesXmlDocument = DocumentUtils.createDocument( mappingFile, false );
		List distributedComponentsList = DocumentUtils.getNodes( queriesXmlDocument.getFirstChild(),
				"DistributedComponent" );
		if ( distributedComponentsList == null || distributedComponentsList.isEmpty() )
			throw new RuntimeException( "Must exist at leats one DistributedComponent" );
		

		for ( int i = 0; i < distributedComponentsList.size(); i++ ) {
			DistributedComponent tempComponent = EasyAcceptMappingReader
					.populateDistributedComponent( (Node) distributedComponentsList.get( i ) );
			componentsMap.put( tempComponent.getHostLabel(), tempComponent );
		}

		return componentsMap;
	}


	/**
	 * Populate a DistributedComponent object
	 * 
	 * @param xmlNode
	 * @return A new instance of distributed component
	 */
	private static DistributedComponent populateDistributedComponent( Node xmlNode ) {

		DistributedComponent component = new DistributedComponent();
		Node tempNode = null;
		try{
			tempNode = (Node) DocumentUtils.getNodes( xmlNode, "host_label" ).get( 0 );
			component.setHostLabel( DocumentUtils.getValue( tempNode ).trim() );
		}catch (Exception e) {
			throw new RuntimeException("Problems parsing XML file: missing <host_label/> "+xmlNode.getBaseURI());
		}

		try{
			tempNode = (Node) DocumentUtils.getNodes( xmlNode, "facade" ).get( 0 );
			component.setFacade( DocumentUtils.getValue( tempNode ).trim() );
		}catch (Exception e) {
			throw new RuntimeException("Problems parsing XML file: missing <facade/> "+xmlNode.getBaseURI());
		}

		try{
			tempNode = (Node) DocumentUtils.getNodes( xmlNode, "jar" ).get( 0 );
			component.setJarPath( DocumentUtils.getValue( tempNode ).trim() );
		}catch (Exception e) {
			throw new RuntimeException("Problems parsing XML file: missing <jar/> "+xmlNode.getBaseURI());
		}
		
		try{
			tempNode = (Node) DocumentUtils.getNodes( xmlNode, "host_address" ).get( 0 );
			component.setHostAddress( DocumentUtils.getValue( tempNode ).trim() );
		}catch (Exception e) {
			throw new RuntimeException("Problems parsing XML file: missing <host_address/> "+xmlNode.getBaseURI());
		}
		
		try{
			tempNode = (Node) DocumentUtils.getNodes( xmlNode, "host_port" ).get( 0 );
			component.setHostPort( Integer.parseInt( DocumentUtils.getValue( tempNode ).trim() ) );
		}catch (Exception e) {
			throw new RuntimeException("Problems parsing XML file: missing <host_port/> "+xmlNode.getBaseURI());
		}
		
		try{
			tempNode = (Node) DocumentUtils.getNodes( xmlNode, "args" ).get( 0 );
			component.setArgs( DocumentUtils.getValue(tempNode).trim() );
		}catch (Exception e) {
			throw new RuntimeException("Problems parsing XML file: missing <args/> "+xmlNode.getBaseURI());
		}
		
//		tempNode = (Node) DocumentUtils.getNodes( xmlNode, "properties" ).get( 0 );
//		component.setPropertiesPath( DocumentUtils.getValue( tempNode ).trim() );

		return component;
	}

}
